
package core;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import task.Calculator;

public class Main {
    public static void main(String args[]) {
        Thread threads[] = new Thread[10];
        Thread.State states[] = new Thread.State[10];
        for (int i = 0; i < 10; i++) {
            threads[i] = new Thread(new Calculator(i));
            if (i % 2 == 0) {
                threads[i].setPriority(Thread.MAX_PRIORITY);
            } else {
                threads[i].setPriority(Thread.MIN_PRIORITY);
            }
            threads[i].setName("task" + i);
        }

        try {
            
            FileWriter fileWriter = new FileWriter(".\\data\\log.txt",true);
            
            PrintWriter printWriter = new PrintWriter(fileWriter);
            for (int i = 0; i < 10; i++) {
                printWriter.println(threads[i].getName() + "----state---" + threads[i].getState());
                states[i] = threads[i].getState();
            }
            for (int i = 0; i < states.length; i++) {
                threads[i].start();
            }
            boolean finish = false;
            while(!finish){
                for (int i = 0; i < 10; i++) {
                    if(states[i]!=threads[i].getState()){
                        printWriter.println(threads[i].getName()+":  before :"+states[i]+"   after :"+threads[i].getState());
                        states[i]=threads[i].getState();
                    }
                }
                finish = true;
                for(int i =0;i<10;i++){
                    finish = (finish&&threads[i].getState()==Thread.State.TERMINATED);
                }
                
            }
            printWriter.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
